/*
 * @lc app=leetcode.cn id=1002 lang=javascript
 *
 * [1002] 查找常用字符
 */

// @lc code=start
/**
 * @param {string[]} A
 * @return {string[]}
 */
var commonChars = function (A) {
    let map1={}
    let map2={}
    for(let i=0;i<A.length;i++){
        for(let j=0;j<A[i].length;j++){
            let temp=A[i][j]
            if(map2[temp]){
                map2[temp]++
            }else{
                map2[temp]=1
            }
        }
        if(i==0){
            map1=Object.assign({},map2)
        }else{
            for(let i in map1){
                if(i in map2){
                    map1[i]=Math.min(map2[i],map1[i])
                }else{
                    delete map1[i]
                }
            }
        }
        map2={}
    }
    let res=[]
    for(let i in map1){
        while(map1[i]){
            res.push(i)
            map1[i]--
        }
    }
    return res
};
// @lc code=end

commonChars(["bella","label","roller"])
var commonChars = function(A) {
    let ans = [], word = A[0], slen = word.length
    for(let s of word){
        if(A.every(m => m.includes(s))) {
            A = A.map(m => m.replace(s, ''))
            ans.push(s)
        }
    }
    return ans
};

